﻿using Flame.Dlr;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Flame.Reflection
{
    public abstract class AIntellisense : IIntellisense
    {
        protected IExecutable _compiler;
        public AIntellisense(IExecutable rubycompiler) { _compiler = rubycompiler; }

        protected abstract Type InstanceTypeFromName(string word);
        protected abstract Type ClassTypeFromName(string word);
        protected abstract string[] ClassTypeFromName_More(string word);


        protected abstract string[] Split(string word);
        protected abstract string Clean(string word);

        protected abstract IntellisenseParsing IntellisenseParsing_Method { get; }
        protected abstract IntellisenseParsing IntellisenseParsing_Type { get; }

        public Type GetType(string svalue)
        {
            if (svalue == "") return null;
            try
            {
                return ClassTypeFromName(svalue);
            }
            catch
            {
                try
                {
                    return InstanceTypeFromName(svalue);
                }
                catch
                {
                    try
                    {
                        //return compiler.ClassTypeFromName_More(svalue);
                    }
                    catch
                    {
                        return null;
                    }
                }
            }

            return null;
        }

        public IntellisenseResult GetMatchResults(string text)
        {
            string spartialvalue;
            string svalue;
            bool method;
            GetMatchResults(text, out svalue, out spartialvalue, out method);
            string[] names = (from x in Split(svalue) where x != "" select x).ToArray();
            IntellisenseResult.ResultType t;
            if (method) t = IntellisenseResult.ResultType.InsideMethod;
            else t = IntellisenseResult.ResultType.Type;

            return new IntellisenseResult() { FullName = Clean(svalue), Names = names, Partialname = spartialvalue, Type = t };
        }

        public void GetMatchResults(string text, out string svalue, out string spartialvalue, out bool method)
        {
            if (text.Length > 0)
            {
                char lastchar = text[text.Length - 1];
                method = lastchar == ',' || lastchar == '(';
                if (method)
                {
                    IntellisenseParsing_Method.GetMatchResults(text, out svalue, out spartialvalue);
                }
                else
                {
                    IntellisenseParsing_Type.GetMatchResults(text, out svalue, out spartialvalue);
                }
            }
            else
            {
                svalue = "";
                spartialvalue = "";
                method = false;
            }
        }


    }


}
